//
//  MOScrollViewPlaceholder.m
//  WCImApp
//
//  Created by alfie on 2019/11/6.
//  Copyright © 2019 WCImApp. All rights reserved.
//

#import "MOScrollViewPlaceholder.h"
#import "UIScrollView+EmptyDataSet.h"
#import "UIView+QAExtension.h"
#import <Masonry/Masonry.h>

@interface MOScrollViewPlaceholder ()  <DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>

/** 占位图按钮 */
@property (strong, nonatomic) UIButton      *placeholderBtn;

/** 自定义占位图按钮 */
@property (strong, nonatomic) UIView        *customView;

@end

@implementation MOScrollViewPlaceholder

- (instancetype)init {
    if (self = [super init]) {
        
        self.titleForBtn = (@"点我刷新");
        self.title = (@"暂无数据");
        self.image = @"defaultpage_nocollection";
        self.title_space_image = 10;
        self.title_space_button = 80;
        self.offset = 0;
        self.shouldShow = YES;
    }
    return self;
}




#pragma mark - DZNEmptyDataSetSource 空白占位图
- (void)setEnable:(BOOL)enable {
    if (_enable != enable) {
        _enable = enable;
        if (_enable) {
            self.scrollView.emptyDataSetDelegate = self;
            self.scrollView.emptyDataSetSource   = self;
        } else {
            self.scrollView.emptyDataSetDelegate = nil;
            self.scrollView.emptyDataSetSource   = nil;
        }
    }
}


- (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView {
    return self.backgroundColor ?: self.scrollView.backgroundColor;
}


- (UIButton *)placeholderBtn {
    if (_placeholderBtn == nil) {
        _placeholderBtn = [UIButton new];
        [_placeholderBtn setTitle:self.titleForBtn forState:UIControlStateNormal];
        [_placeholderBtn setTitleColor:UIColor.whiteColor forState:0];
        [_placeholderBtn setBackgroundColor:[UIColor main_Color]];
        [_placeholderBtn.titleLabel setFont:WCIFontRegular(16)];
        ViewRadius(_placeholderBtn, 8);
        [_placeholderBtn addTarget:self action:@selector(placeholderButtonClick) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _placeholderBtn;
}

- (void)setTitleForBtn:(NSString *)titleForBtn {
    _titleForBtn = titleForBtn;
    if (_placeholderBtn) {
        [_placeholderBtn setTitle:_titleForBtn forState:UIControlStateNormal];
    }
}

// 返回图片
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    
    if (self.showBtn) {
        for (UIView *view in scrollView.subviews) {
            if ([view isKindOfClass:NSClassFromString(@"DZNEmptyDataSetView")]) {
                UIView *contentView = view.subviews.firstObject;
                if (self.customView) {
                    [view addSubview:self.customView];
                    [self.customView mas_makeConstraints:^(MASConstraintMaker *make) {
                       make.top.equalTo(contentView.mas_bottom).offset(self.title_space_button);
                       make.centerX.offset(0);
                       make.width.offset(self.customView.frame.size.width);
                       make.height.offset(self.customView.frame.size.height);
                    }];
                } else {
                    [view addSubview: self.placeholderBtn];
                    self.placeholderBtn.hidden = NO;
                    [self.placeholderBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                        make.top.equalTo(contentView.mas_bottom).offset(self.title_space_button);
                        make.left.offset(32);
                        make.right.offset(-32);
                        make.height.offset(50);
                    }];
                }
                break;
            }
        }
    } else {
        _placeholderBtn.hidden = YES;
        for (UIView *view in scrollView.subviews) {
           if ([view isKindOfClass:NSClassFromString(@"DZNEmptyDataSetView")]) {
               view.frame = CGRectMake(0, 0, self.scrollView.size.width, self.scrollView.frame.size.height);
               break;
           }
        }
    }
    return [self.image isKindOfClass:UIImage.class] ? self.image : [UIImage imageNamed:self.image];
}


//返回标题文字
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    return [[NSAttributedString alloc] initWithString:self.title attributes:@{NSFontAttributeName: WCIFontRegular(14), NSForegroundColorAttributeName: assist_text_color}];
}


//点击占位按钮
- (void)placeholderButtonClick {
    if (self.placeholderHandle) {
        self.placeholderHandle();
    }
}


//是否允许滚动
- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView {
    return YES;
}

- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView {
    return _shouldShow;
}


// 图片与标题文本的距离
- (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView {
    return self.title_space_image;
}


//调整整体的垂直偏移量
- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {
    return self.offset - 100;
}


- (void)replaceButtonWithCustomView:(UIView *)customView {
    self.customView = customView;
}


@end
